package com.cxyzy.demo.ui.activity

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.cxyzy.demo.R
import com.cxyzy.demo.databinding.ActivityUserBinding
import com.cxyzy.demo.db.bean.User
import com.cxyzy.demo.ui.adapter.UserAdapter
import com.cxyzy.demo.utils.InjectorUtils
import com.cxyzy.demo.viewmodels.UserViewModel

class UserActivity : AppCompatActivity() {
    private lateinit var viewModel: UserViewModel
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding: ActivityUserBinding = DataBindingUtil.setContentView(this, R.layout.activity_user)
        val factory = InjectorUtils.provideUserViewModelFactory(this)
        viewModel = ViewModelProviders.of(this, factory).get(UserViewModel::class.java)
        val adapter = UserAdapter()
        adapter.listener = View.OnClickListener { view ->
            view?.let {
                val user = view.tag as User
                viewModel.delUser(user.id)
            }
        }

        binding.userRv.adapter = adapter
        viewModel.userList.observe(this, Observer { adapter.submitList(it) })
    }
}
